package com.bckj.fastboot.core.rpc.feign;

import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

@Slf4j
public class JavaSerialization {

    public JavaSerialization() {
    }

    public static byte[] serialize(Object obj) {
        ByteArrayOutputStream os = new ByteArrayOutputStream();

        try {
            ObjectOutputStream out = new ObjectOutputStream(os);
            out.writeObject(obj);
            out.close();
        } catch (Exception var3) {
            log.error("java serialize failed," + var3);
        }

        return os.toByteArray();
    }

    public static Object deserialize(byte[] bytes) {
        ByteArrayInputStream is = new ByteArrayInputStream(bytes);
        Object value = null;

        try {
            ObjectInputStream in = new ObjectInputStream(is);
            value = in.readObject();
            in.close();
        } catch (Exception var4) {
            log.error("java deserialize failed," + var4);
        }

        return value == null ? bytes : value;
    }
}
